<?php
global $wpdb;
global $post;
global $wp_query;

// displays the blog header
get_header();

if ($wp_query->query_vars['watw_type'] == 'date') {
	// get the where clause to find by date (yyyy-mm)
	$searchDate = $_GET['watw_year'] . '-' . $_GET['watw_month'];
	
	$sql = "
	    SELECT wposts.*, wptravel.*
	    FROM " . $wpdb->posts . " wposts, " . $wpdb->prefix . "wptravel wptravel
	    WHERE wposts.ID = wptravel.post_ID 
	    AND wposts.post_status = 'publish' 
	    AND wposts.post_type = 'post' 
	    AND CONCAT(YEAR(wptravel.startDate), '-', MONTH(wptravel.startDate)) = '" . $searchDate . "'
	    AND wptravel.closed = 'n'
	    GROUP BY wptravel.post_ID
	    ORDER BY wptravel.startDate ASC
	";
} else if ($wp_query->query_vars['watw_type'] == 'price'){
	$ranges = array();
	$ranges[1] = array('low' => 1, 'high' => 499);
	$ranges[2] = array('low' => 500, 'high' => 999);
	$ranges[3] = array('low' => 1000, 'high' => 1499);
	$ranges[4] = array('low' => 1500, 'high' => 1999);
	$ranges[5] = array('low' => 2000, 'high' => 1000000);
	
	// get the where clause to find by price range
	$sql = "
	    SELECT wposts.*, wptravel.*
	    FROM " . $wpdb->posts . " wposts, " . $wpdb->prefix . "wptravel wptravel
	    WHERE wposts.ID = wptravel.post_ID 
	    AND wposts.post_status = 'publish' 
	    AND wposts.post_type = 'post' 
	    AND wptravel.price BETWEEN " . $ranges[$_GET['watw_range']]['low'] . " AND " . $ranges[$_GET['watw_range']]['high'] . "
	    AND wptravel.closed = 'n'
	    GROUP BY wptravel.post_ID
	    ORDER BY wptravel.startDate ASC
	";
}

$pageposts = $wpdb->get_results($sql, OBJECT);

?>


	<div id="content" class="archive">
		<h2 class="title"><?php _e('Search Results','arthemia'); ?></h2>
		<div class="clearfloat">

<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
			<div class="tanbox left">
				<span class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></span>
			</div>
			<div>
				<?php the_excerpt(); ?>
			</div>
<?php endforeach; ?>
<?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
		</div>
	</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>